#include "CPistol.h"

#define PISTOL_FILE			"Pistol_30x5_1_2.png"
#define BULLET_TANK_FILE	"bullet.png"
#define BULLET_SMOCK_FILE	"Smock.png"

#define X_VERLOCITY 2.0f
#define Y_VERLOCITY 0.5f

#define LEFT 1
#define RIGHT 0

#define START_BULLET	0
#define PISTOL_MARCO	1
#define BULLET_TANK		2
#define BULLET_SMOCK	3
#define ANIMATION 30

CPistol::CPistol(LPD3DXSPRITE spriteHandler, float scale): Object()
{
	m_Sprite = new CSprite[4];

	m_breleaseSmock = false;

	m_nposX = 0;
	m_nposY = 0;
	m_nAndr = 0;
	m_nframeW = 30;
	m_nframeH = 5;

	m_fverX= X_VERLOCITY;
	m_fverY = 0;

	m_ndirection = RIGHT;
	m_nanimationRate = ANIMATION;
	m_fto = 0.0f;
	m_fscaling = scale;
	loadSoucre(spriteHandler);
	setRect(0);
	m_npistolIndex = 0;
}

CPistol::~CPistol()
{

}

void CPistol::loadSoucre(LPD3DXSPRITE spriteHandler)
{
	m_Sprite[START_BULLET].CreateSprite(spriteHandler, NULL, 0, 0, 1, 0, 0, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[PISTOL_MARCO].CreateSprite(spriteHandler, PISTOL_FILE, m_nframeW, m_nframeH, 1, 2, m_fscaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[BULLET_TANK].CreateSprite(spriteHandler, BULLET_TANK_FILE, 20, 20, 22, 24, m_fscaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[BULLET_SMOCK].CreateSprite(spriteHandler, BULLET_SMOCK_FILE, 80, 160, 32, 33, m_fscaling, D3DCOLOR_XRGB(255, 255, 255));
}
void CPistol:: setDirection(int idirection)
{
	m_ndirection = idirection;
	m_Sprite->SetSpriteLine(m_ndirection);
}

void CPistol::Render()
{
	m_Sprite[m_npistolIndex].Render(m_nposX, m_nposY);
}

void CPistol::Update(int deltaTime, int moveCame)
{
	
	DWORD now = GetTickCount();

	if(m_npistolIndex == BULLET_TANK && m_Sprite[m_npistolIndex].GetSpriteLine() != 0)
		m_fto += 0.2f;

	float  t = m_fto;

	m_nAndr += m_fverX*deltaTime;
	m_nposX = m_nAndr - moveCame;

	char str[25];
	itoa(m_nposX, str, 10);
	if (m_nposX > 850)
		MessageBox(NULL, str , "Error", MB_OK);

	if(m_npistolIndex == BULLET_TANK)
		m_nposY += t*t;
	else
		m_nposY += m_fverY*deltaTime;

	if (now - m_dwlastUpdate >= 1000/ANIMATION)
	{
		Next();
		m_dwlastUpdate = GetTickCount();
	}
	setRect(moveCame);
}

void CPistol::Next()
{
	m_Sprite[m_npistolIndex].Next();

	if(m_Sprite[m_npistolIndex].GetIndex() == 2 && m_npistolIndex == PISTOL_MARCO)
		m_Sprite[m_npistolIndex].SetIndex(0);

	if(m_Sprite[m_npistolIndex].GetIndex() == 13 && m_npistolIndex == BULLET_TANK && m_Sprite[m_npistolIndex].GetSpriteLine() == 0)
		m_Sprite[m_npistolIndex].SetIndex(0);

	if(m_Sprite[m_npistolIndex].GetIndex() == 21 && m_npistolIndex == BULLET_TANK && m_Sprite[m_npistolIndex].GetSpriteLine() == 1)
		m_Sprite[m_npistolIndex].SetIndex(0);

	if(m_Sprite[m_npistolIndex].GetIndex() == 16 && m_npistolIndex == BULLET_TANK && m_Sprite[m_npistolIndex].GetSpriteLine() == 2)
 		m_Sprite[m_npistolIndex].SetIndex(0);

	if(m_Sprite[m_npistolIndex].GetIndex() == 31 && m_npistolIndex == BULLET_SMOCK)
	{
		m_breleaseSmock = true;
		m_Sprite[m_npistolIndex].SetIndex(0);
	}
}

void CPistol::setRect(int moveCame)
{
	m_ObRect = new RECT();
	m_ObRect->left = m_nposX + moveCame;
	m_ObRect->right = m_ObRect->left + 60;
	m_ObRect->top = m_nposY - 45;
	m_ObRect->bottom = m_ObRect->top + 10;
}
//get methods
//
bool CPistol::IsFinish()
{
	return (m_Sprite->GetIndex() == m_Sprite->GetPerRow());
}

//function
void CPistol::bullet()
{
	m_npistolIndex = PISTOL_MARCO;
	m_fverX = 1.3f;
	m_Sprite[m_npistolIndex].SetIndex(0);
}

void CPistol::bulletTank()
{
	m_npistolIndex = BULLET_TANK;
	m_fverX = 0.0f;
	m_fto = 0;
	m_Sprite[m_npistolIndex].SetIndex(0);
	m_Sprite[m_npistolIndex].SetSpriteLine(0);
}

void CPistol::fire()
{
	m_npistolIndex = BULLET_TANK;
	m_fverX = -1.3f;
	m_fto = 0;
	m_Sprite[m_npistolIndex].SetIndex(0);
	m_Sprite[m_npistolIndex].SetSpriteLine(1);
}

void CPistol::shells()
{
	m_npistolIndex = BULLET_TANK;
	m_fverX = 0.3f;
	m_fto = 0;
	m_Sprite[m_npistolIndex].SetIndex(0);
	m_Sprite[m_npistolIndex].SetSpriteLine(2);
}

void CPistol::smock()
{
	m_npistolIndex = BULLET_SMOCK;
	m_fverX = 0.0f;
	m_Sprite[m_npistolIndex].SetIndex(0);
}

void CPistol::release()
{
	m_nframeW = 0;
	m_nframeH = 0;
}